<?php

/**
 * 文件由尚数增量代码引擎生成
 * @author: Shann 851188611@qq.com
 */

namespace app\admin\controller;

class BannerBase
{
    public function index()
    {
        $banners = \app\model\Banner::field(['id', 'title', 'sort', 'file_id']);

        $banners->order('id', 'desc');

        $banners->with(['image' => function ($query) {
            $query->field(['id', 'url']);
        }]);

        $data['banners'] = $banners->select();

        return json(['code' => 20000, 'data' => $data]);
    }

    public function detail()
    {
        $banner = \app\model\Banner::field(['id', 'title', 'sort']);

        if (request()->has('id')) {
            $banner->where('id', input('id'));
        }

        $data['banner'] = $banner->find();

        return json(['code' => 20000, 'data' => $data]);
    }

    public function create()
    {
        $param = request()->param();

        $model = new \app\model\Banner();

        $model->save($param);

        $data['banner'] = \app\model\Banner::find($model->id);

        return json(['code' => 20000, 'msg' => 'success', 'data' => $data]);
    }

    public function update()
    {
        $param = request()->param();

        $model = \app\model\Banner::find(input('id'));

        $model->save($param);

        return json(['code' => 20000, 'msg' => 'success']);
    }

    public function delete()
    {
        \app\model\Banner::destroy(function ($query) {

        });

        return json(['code' => 20000, 'msg' => 'success']);
    }
}
